﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard;
using Bootstrap.Models;
using Bootstrap.Services;

namespace Bootstrap.Builders {
    public class BuilderDescriptor {
       
        public Func<IBootstrapSettingsService, FeatureDescriptor, IEnumerable<string>> Builder { get; set; }

        public BuilderDescriptor WithBuilder(Func<IBootstrapSettingsService, FeatureDescriptor, IEnumerable<string>> builder) {
            Builder = builder;
            return this;
        }
    }

    public class BuilderDescriptionContext {
        public IDictionary<string, Func<BuilderDescriptor, BuilderDescriptor>> Builders { get; private set; }
        public BuilderDescriptionContext() {
            Builders = new Dictionary<string, Func<BuilderDescriptor, BuilderDescriptor>>();
        }
        public BuilderDescriptionContext Describe(string name, Func<BuilderDescriptor,BuilderDescriptor> builder) {
            Builders.Add(name, builder);
            return this;
        }
    }

    public interface IBootstrapFeatureBuilder : IDependency {
        void Describe(BuilderDescriptionContext context);
    }

    public class ImportBuilder : IBootstrapFeatureBuilder {
        public void Describe(BuilderDescriptionContext context) {
            context.Describe(
                "Import",
                builder =>
                builder
                    .WithBuilder(
                        (settings, feature) => 
                        feature.Action(settings)
                        .Select(
                            import => 
                            String.Format("@import \"{0}\";\n", (object) import)
                        )
                    )
                );
        }
    }

    public class InsertBuilder : IBootstrapFeatureBuilder {
        public void Describe(BuilderDescriptionContext context) {
            context.Describe(
                "Insert",
                builder =>
                builder.WithBuilder(
                    (settings, feature) =>
                    feature.Action(settings)
                )
            );
        }
    }
}